package chess

//给边界做限制

type LimitLocation interface {
	limit() bool
}

// 所有棋子的范围
func (p *Piece) limit() bool {
	if p.Row >= 1 && p.Row <= 10 && p.Line >= 1 && p.Line <= 9 {
		return true
	}
	return false
}

// 象的范围
func (e *Elephant) limit() bool {
	if e.Color == "black" {
		if pointEqual(e.Point, Point{1, 3}) || pointEqual(e.Point, Point{1, 7}) ||
			pointEqual(e.Point, Point{3, 1}) || pointEqual(e.Point, Point{3, 5}) || pointEqual(e.Point, Point{3, 9}) ||
			pointEqual(e.Point, Point{5, 3}) || pointEqual(e.Point, Point{5, 7}) {
			return true
		}
	}
	if e.Color == "red" {
		if pointEqual(e.Point, Point{10, 3}) || pointEqual(e.Point, Point{10, 7}) ||
			pointEqual(e.Point, Point{8, 1}) || pointEqual(e.Point, Point{8, 5}) || pointEqual(e.Point, Point{8, 9}) ||
			pointEqual(e.Point, Point{6, 3}) || pointEqual(e.Point, Point{6, 7}) {
			return true
		}
	}
	return false
}

// 士的范围
func (g *Guard) limit() bool {
	if g.Color == "black" {
		if pointEqual(g.Point,
			Point{1, 4}, Point{1, 6},
			Point{2, 5},
			Point{3, 4}, Point{3, 6}) {
			return true
		}
	}
	if g.Color == "red" {
		if pointEqual(g.Point,
			Point{10, 4}, Point{10, 6},
			Point{9, 5},
			Point{8, 4}, Point{8, 6}) {
			return true
		}
	}
	return false
}

// 国王的范围
func (k *King) limit() bool {
	if k.Color == "black" {
		if pointEqual(k.Point,
			Point{1, 4}, Point{1, 5}, Point{1, 6},
			Point{2, 4}, Point{2, 5}, Point{2, 6},
			Point{3, 4}, Point{3, 5}, Point{3, 6}) {
			return true
		}
	}
	if k.Color == "black" {
		if pointEqual(k.Point,
			Point{10, 4}, Point{10, 5}, Point{10, 6},
			Point{9, 4}, Point{9, 5}, Point{9, 6},
			Point{8, 4}, Point{8, 5}, Point{8, 6}) {
			return true
		}
	}
	return false
}

// pointEqual 判断bePoint中是否有与point1相等的点
func pointEqual(point1 Point, bePoint ...Point) bool {
	for _, tempPoint := range bePoint {
		if point1.Row == tempPoint.Row && point1.Line == tempPoint.Line {
			return true
		}
	}
	return false
}

//判断点在不在范围内
func (p Point) withinRange() bool {
	if p.Row >= 1 && p.Row <= 9 && p.Line >= 1 && p.Line <= 10 {
		return true
	}
	return false
}

// AtARowORALine 当前点和point在不在一行或者一列
func (p *Piece) AtARowORALine(point Point) bool {
	if p.Row != point.Row && p.Line != point.Line {
		return false
	}
	return true
}
